package com.sweets.look.constant;

import android.content.Context;
import android.os.Build;

import com.sweets.look.R;


public class JS {
    public static final String HIDE_UNDER = "javascript:var es=document.getElementsByTagName(\'a\');for(i in es){es[i].style.textDecoration=\'none\';}";
    public static final String IMG_REFLOW = "javascript:var es=document.getElementsByTagName(\'img\');for(i in es){es[i].style.maxWidth=\'100%\';}";
    public static final String SOURCE = "javascript:alert(document.documentElement.outerHTML)";
    public static final String TEXT_REFLOW = "javascript:document.getElementsByTagName(\'body\')[0].style.width=window.innerWidth+\'px\';";


    public static String copyLinkText(String url) {
        return "javascript: url=unescape(\'" + url + "\');" + "var ls=document.getElementsByTagName(\'a\');"
                + "for(var i=0,l=ls.length;i<l;i++){" + "var el=ls[i];" + "if(unescape(el.href)==url){"
                + "var txt=el.innerText;" + "window.sweets.getWebText(txt);" + "break}" + "};";
    }

    /**
     * 删除标签<过滤广告>
     *
     * @param tags
     * @return
     */
    public static String deleteTags(String tags) {
        StringBuilder sb = new StringBuilder();
        sb.append("javascript: ");
        String[] alltag = tags.split(",");
        for (int i = 0; i < alltag.length; i++) {
            String adTag = alltag[i];
            if (adTag.trim().length() > 0) {
                adTag = adTag.trim();
                if (adTag.contains("#")) {
                    adTag = adTag.substring(adTag.indexOf("#") + 1);
                    if (Build.VERSION.SDK_INT <  Build.VERSION_CODES.KITKAT) {//19
                        sb.append("document.getElementById(\'").append(adTag).append("\').innerHTML=\'\';");
                    } else {
                        sb.append("document.getElementById(\'").append(adTag).append("\').remove();");
                    }

                } else if (adTag.contains(".")) {
                    adTag = adTag.substring(adTag.indexOf(".") + 1);
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                        sb.append("var esc=document.getElementsByClassName(\'").append(adTag).append("\');for (i in esc){esc[i].innerHTML=\'\';};");

                    } else {
                        sb.append("var esc=document.getElementsByClassName(\'").append(adTag).append("\');for (var i = esc.length - 1; i >= 0; i--){esc[i].remove();};");

                    }
                } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                    sb.append("var esc=document.getElementsByTagName(\'").append(adTag).append("\');for (i in esc){esc[i].innerHTML=\'\';};");
                } else {
                    sb.append("var esc=document.getElementsByTagName(\'").append(adTag).append("\');for (var i = esc.length - 1; i >= 0; i--){esc[i].remove();};");
                }
            }
        }
        return sb.toString();
    }

    public static String markAds(Context mContext, String url) {
        return "javascript: url = unescape(\'" + url + "\');" + "var ls = document.getElementsByTagName(\'a\');"
                + "var el = null;" + "for (var i = 0, l = ls.length; i < l; i++) {" + "el = ls[i];"
                + "if(unescape(el.href) == url || unescape(el.href) == (url + \'/\')){" + "break;" +
                "}else{" + "el = null;" + "};" + "};" + "if(!el){" + "ls = document.getElementsByTagName(\'img\');"
                + "el = null;" + "for (var i = 0, l = ls.length; i < l; i++) { " + "el = ls[i];" +
                "if(unescape(el.src) == url || unescape(el.src) == (url + \'/\')){" + "break;" + "}else{"
                + "el = null;" + "};" + "};" + "};" + "if(el){" + "var el2 = el;" + "var pl  = 0;" +
                "while(true){" + "if(el2.id || el2.className){" + "break;" + "}else{" + "el2 = el2.parentNode;"
                + "pl++;" + "};" + "};" + "if(el2){" + "el = el2;" + "while(true){" + "el2 = el2.parentNode;"
                + "if(!el2 || el2.childElementCount>1){" + "break;" + "};" + "if(el2.id || el2.className){"
                + "el = el2;" + "};" + "};" + "el2 = el;" + "var elk = (el2.id ? \'#\'+el2.id: \'.\'+el2.className);"
                + "if(confirm(\'" + mContext.getResources().getString(R.string.dialog_confirm_block) + "(\'+el2.tagName+elk+\')\')){"
                + "el2.style.display=\'none\';" + "var ht = location.host;" + "window.sweets.record(ht, elk);"
                + "};" + "};" + "};";
    }

    public static String runCustomCSS(String css) {
        return "javascript:if(true){var d = document; var s = d.createElement(\'style\'); s.setAttribute(\'type\', \'text/css\'); var t = d.createTextNode(\'"
                + css + "\'); s.appendChild(t); d.head.appendChild(s);};";
    }

    public static String translate(int checkedId) {
        String language = "en";
        switch (checkedId) {
            case 0: {
                language = "zh-chs";
                break;
            }
            case 1: {
                language = "zh-tw";
                break;
            }
            case 2: {
                language = "en-us";
                break;
            }
            case 3: {
                language = "en-gb";
                break;
            }
            case 4: {
                language = "ru";
                break;
            }
            case 5: {
                language = "es";
                break;
            }
            case 6: {
                language = "uk";
                break;
            }
        }

        return "javascript:if(typeof%20zZxwnC60==typeof%20alert)zZxwnC60();(function(){var%20s%20=%20document.createElement(\'script\');%20s.type%20=%20\'text/javascript\';%20s.src%20=%20\'http://labs.microsofttranslator.com/bookmarklet/default.aspx?f=js&to="
                + language + "\';%20document.body.insertBefore(s,%20document.body.firstChild);})()";
    }
}

